package com.jiepos.mpos.biz.channel.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jiepos.mpos.common.constant.DBConstant.Table;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.common.dao.InfChannelDao;
import com.jiepos.mpos.common.entity.App_Inf_ChannelEntity;
import com.jiepos.mpos.core.entity.Pagination;
import com.jiepos.mpos.core.service.BaseService;
import com.jiepos.mpos.core.util.IDFactoryUtil;
import com.jiepos.mpos.core.util.StringUtils;

/**
 * 
 * 
 * <p>标题: —— 通道信息表服务类</p>
 * <p>模块: 通用平台</p>
 * <p>版权: Copyright (c) 2017 江苏杰博实信息技术有限公司
 * @ProjectName:  [mpos] 
 * @Package com.jiepos.mpos.biz.channel.service 
 * @author 张学涛
 * @version 1.0
 * @since 2017年9月20日下午5:20:28
 * @history（历次修订内容、修订人、修订时间等）
 */
@Service
public class InfChannelService extends BaseService {
	
	@Autowired
	private InfChannelDao infChannelDao;
	
	/**
	 * 查询通道信息列表json分页数据
	 * @param pagination
	 * @param infChannel
	 * @return
	 */
	public Map getChannelInfList(Pagination pagination,App_Inf_ChannelEntity infChannel){
		StringBuffer sb = getInitSql("t1").append(",");
		sb.append("t1.channel_uuid as uuid,t1.channel_no,t1.chn_nm,t1.chn_tp,t1.chn_prmtb,t1.trans_interval,t1.change_mchnt_tm from ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(infChannel.getChannel_no())) {
			sb.append( " and t1.channel_no like ? " );
			list.add("%" + infChannel.getChannel_no() + "%");
		}
		pagination.queryString = sb.toString();
		pagination = this.getOrderSql(pagination, "t1");
		return infChannelDao.getPaginationMap(pagination, "t1", list);
	}
	
	/**
	 * 保存通道信息
	 * @param infChannel
	 * @param map
	 */
	public void saveChannelInf(App_Inf_ChannelEntity infChannel,Map map){
		infChannel.setRec_upd_usr_id(String.valueOf(map.get(SystemConstant.SESSION_LOGIN_USER)));
		infChannel.setChn_prmtb(infChannel.getChn_prmtb().toUpperCase());
		if (!StringUtils.isBlank(infChannel.getChannel_uuid())) {
			App_Inf_ChannelEntity newChannelInf=infChannelDao.findByParam("channel_uuid", infChannel.getChannel_uuid());			
			infChannel.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChannel.setRec_id(newChannelInf.getRec_id());
			infChannel.setLast_oper_in(SystemConstant.OPER_UPDATE);
			List noChangeList = new ArrayList();
			noChangeList.add("channel_uuid");
			noChangeList.add("rec_crt_ts");
			infChannelDao.update(infChannel,noChangeList);
		}else{
			infChannel.setRec_crt_ts(new Timestamp(System.currentTimeMillis()));
			infChannel.setRec_upd_ts(new Timestamp(System.currentTimeMillis()));
			infChannel.setChannel_uuid(IDFactoryUtil.getUUID());			
			infChannel.setRec_st(SystemConstant.NORMAL_STATUS);
			infChannel.setLast_oper_in(SystemConstant.OPER_ADD);
			infChannelDao.save(infChannel);
		}
	}
	
	/**
	 * 根据记录id查询实体对象
	 * @param channel_uuid
	 * @return
	 */
	public App_Inf_ChannelEntity getChannelInfById(String channel_uuid){
		if (StringUtils.isBlank(channel_uuid) || channel_uuid.equals("undefined")) {
			return new App_Inf_ChannelEntity();
		}
		return infChannelDao.findByParam("channel_uuid", channel_uuid);
	}
	
	/**
	 * 根据记录id集合删除数据
	 * @param channel_uuids
	 */
	public void deleteChannelInfByIds(String channel_uuids){
		infChannelDao.deleteByStatus("channel_uuid", channel_uuids);
	}
	
	
	/**
	 * 校验通道信息
	 * @param infChannel
	 * @return
	 */
	public String checkChannelInf(App_Inf_ChannelEntity infChannel){
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.channel_uuid,t1.channel_no,t1.chn_nm from ");		
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t1 where 1=1 ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(infChannel.getChannel_no())) {
			sb.append( " and t1.channel_no = ? " );
			list.add(infChannel.getChannel_no());
		}			
		if (!StringUtils.isBlank(infChannel.getChannel_uuid())) {
			sb.append( " and t1.channel_uuid != ? " );
			list.add(infChannel.getChannel_uuid());
		}		
		List result1=infChannelDao.queryForList(sb.toString(), list);
		if(result1==null||result1.size()==0){
			sb = new StringBuilder();
			sb.append("select TABLE_NAME,TABLESPACE_NAME from user_tables  where TABLE_NAME='");						
			sb.append(infChannel.getChn_prmtb().toUpperCase());
			sb.append("'");
			List result2=infChannelDao.queryForList(sb.toString());
			if(result2==null||result2.size()==0){
				return SystemConstant.FAIL_1;
			}
			return SystemConstant.SUCCESS;
		}
		return SystemConstant.FAIL_2;
	}
	
	/**
	 * 根据通道获取对应表
	 * @param channel_no
	 * @param chn_tp
	 * @return
	 */
	public Map getTableNameByParam(String channel_no){
		if(StringUtils.isBlank(channel_no)||channel_no.equals("undefined")){
			return new HashMap();
		}
		StringBuilder sb = new StringBuilder();
		sb.append("select t1.channel_no,t1.channel_no||'-'||t1.chn_nm as chnname,t1.chn_tp,t1.chn_prmtb from ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t1 where t1.rec_st!='0' ");
		List list = new ArrayList();
		if (!StringUtils.isBlank(channel_no)) {
			sb.append( " and t1.channel_no = ? " );
			list.add(channel_no);
		}			
		List<Map<String,Object>> result=infChannelDao.queryForList(getOrderSql(sb.toString(), "t1"), list);
		if(result==null||result.size()==0){
			return null;
		}
		return result.get(0);
	}
	
	/**
	 * 根据通道类型获取通道
	 * @param chn_tp
	 * @return
	 */
	public List<Map<String,Object>> getChannelByTp(String chn_tp){
		StringBuffer sb = new StringBuffer();
		sb.append("select t1.channel_no as id,t1.channel_no||'-'||t1.chn_nm  as text from ");
		sb.append(Table.T_APP_INF_CHANNEL);
		sb.append(" t1 where 1=1 ");
		
		List list = new ArrayList();
		if (!StringUtils.isBlank(chn_tp)) {
			sb.append( " and t1.chn_tp like ? " );
			list.add(chn_tp+"%");
		}	
		sb.append(" and t1.rec_st!='");
		sb.append(SystemConstant.DEL_STATUS);
		sb.append("' order by t1.channel_no desc ");
		List<Map<String,Object>> result=infChannelDao.queryForList(sb.toString(), list);	
		return result;
	}
	
	/**
	 * 获取需要进件的通道编号
	 * @return
	 */
	public List<String> getChannelNoForIntoPieces(){
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT T.CHANNEL_NO")
		.append("  FROM ").append(Table.T_APP_INF_CHANNEL).append(" T")
		.append("  LEFT JOIN T_APP_TRANS_LIMIT TL")
		.append("    ON T.CHANNEL_NO = TL.CHANNEL_NO")
		.append(" WHERE T.").append(SystemConstant.REC_ST).append(" = '").append(SystemConstant.NORMAL_STATUS).append("'")
		.append("   AND TL.LIMIT_TYPE = '01'")
		.append("   AND TL.LIMIT_ST = '1'")
		.append("   AND TL.").append(SystemConstant.REC_ST).append(" = '").append(SystemConstant.NORMAL_STATUS).append("'")
		.append("   AND T.REC_ID = (SELECT MIN(REC_ID)")
		.append("                     FROM ").append(Table.T_APP_INF_CHANNEL)
		.append("                    WHERE ").append(SystemConstant.REC_ST).append(" = '").append(SystemConstant.NORMAL_STATUS).append("'")
		.append("                      AND CHN_PRMTB = T.CHN_PRMTB)");
		List<App_Inf_ChannelEntity> list = infChannelDao.queryForListByParam(sql.toString(), new Object[]{});
		if(list == null || list.isEmpty()){
			return null;
		}
		List<String> channelNos = new ArrayList<>();
		for(App_Inf_ChannelEntity entity : list){
			channelNos.add(entity.getChannel_no());
		}
		return channelNos;
	}

}
